package auto.app.config;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Component
@Slf4j
public class RestUtil {

    @Resource
    RestTemplate restTemplate;


    /**
     * DLT专用执行方法
     *
     * @param param  请求参数：可以添加一些常量请求值
     * @param url    访问的url
     * @param method 请求的方法
     * @return
     */
    public String execute(Map<String, Object> param, String url, HttpMethod method) {
//        HttpHeaders headers = this.getDefaultHeader();

        HttpHeaders headers = new HttpHeaders();
        headers.add("content-type", "application/json;charset=UTF-8");
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(param, headers);
        ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }

    public ResponseEntity<String> execute(Map<String, Object> param, String url, HttpMethod method, MultiValueMap<String,String> header) {
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(param, header);
        log.info("打印请求url:");
        log.info(url);
        log.info("打印请求参数:");
        log.info(String.valueOf(param));
        log.info("打印请求头:");
        log.info(String.valueOf(header));
        ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity, String.class);
        log.info("打印返回参数:");
        log.info(response.getBody());
        return response;
    }

}
